package khfx.dao.test.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import khfx.dao.BasicDao;
import khfx.dao.db.Session;
import khfx.dao.test.StudentDao;
import khfx.domain.test.Student;

@Component("studentDao")
public class StudentDaoImpl extends BasicDao implements StudentDao  {

	public void batchUpdate(Session session,List<Student> students) {
		if(students!= null && students.size()>0){
			String sql = "insert into student values(?,?)";
			Object[][] values = new Object[students.size()][2];
			int i = 0;
			for(Student student : students){
				Object[] obj = new Object[2];
				obj[0] = student.getId();
				obj[1] = student.getName();
				
				values[i] = obj;
				i++;
			}
			
			batchUpdate(session, sql, values);
		}
		
	}

	public int count(Session session) {
		Long count = getCount(session, "select count(*) from student ");
		return (int) count.longValue();
	}

	public Student find(Session session, Integer id) {
		String sql = "select * from student where id = ?"; 
		return super.getBeanBySql(session, sql, Student.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Student> findList(Session session) {
		return (List<Student>) getListBeansBySql(session, "select * from student", Student.class);
	}

	public List<Map<String, Object>> findMap(Session session) {
		return getMapsBySql(session, "select * from student");
	}

	public void update(Session session, Integer id) {
		String sql = "update student set name = ? where id = ? ";
		super.update(session,sql , "aaa",id);
	}

	

}
